package com.myt.matp.gui;

import com.myt.matp.core.Event;

import java.lang.Math;

/**
 * Describe class <code>UIPanel</code> here.
 *
 * @author <a href="mailto:emadridm@gmail.com">Enrique Madrid</a>
 * @version 1.0
 */
public final class UIPanel extends UIColumn {

    private UIMenu m_Menu = null;
    private UISoft m_Soft = null;

    private boolean m_ShowMenu = false;

    protected UIPanel(int id, GUI gui) {	
	// Building itself
	super(id, gui);
	setBorder(0);
    }

    public final int getType() {
	return UIWidget.PANEL;
    }

    protected void resized() {
	UIScrollBar sb;

	// Soft menu
	if (hasSoft()) {
	    m_Soft.resize(getViewWidth(), getViewHeight());
	}

	// Vertical Scroll bar
	if (!hasVerticalScroll()) {
	    resizeChildrenToVerticalFix(getViewWidth(), getViewHeight());
	} else {
	    sb = getVerticalScrollBar();
	    sb.resize(getViewWidth(), getViewHeight());
	    resizeChildrenToVerticalScroll(getViewWidth(), getViewHeight());
	}
    }

    protected int getViewHeight() {
	int ret = super.getViewHeight();
	if (hasSoft()) {
	    ret -= m_Soft.getHeight();
	}
	return ret;
    }

    public boolean hasMenu() {
	return (m_Menu != null);
    }

    public UIMenu getMenu() {
	return m_Menu;
    }

    public void setMenu(UIMenu menu) {
	m_Menu = menu;
    }

    public void showMenu() {
	postEvent(Event.UI_MENU, 1);
    }

    public void hideMenu() {
	postEvent(Event.UI_MENU, 0);
    }

    private void setPositionMenu(UIMenu menu) {
	int avgHiMenu = 0;
	int n;
	int avgHiCtrl = 0;
	if (menu != null) {
	    // Dimension
	    if (!menu.isAdjustedInto(this)) {
		menu.adjustIn(60 * getViewWidth() / 100, 
			      60 * getViewHeight() / 100);
		menu.setAdjustedTo(this);
	    }
	    // Position 
	    menu.setPosX(getViewX());
	    menu.setPosY(getViewY() + getViewHeight() - menu.getHeight());
	}
    }

    public boolean hasSoft() {
	return (m_Soft != null);
    }

    public void setSoft(UISoft soft) {
	m_Soft = soft;
	m_Soft.setWindow(3);
    }

    public UISoft getSoft() {
	return m_Soft;
    }

    public boolean dispatchEventUIContainer(final Event e) {
	boolean ret = true;
	if (!dispatchEventUIPanel(e)) {
	    ret = super.dispatchEventUIContainer(e);
	}
	return ret;
    }

    private boolean dispatchEventUIPanel(final Event e) {
	boolean ret = false;

        //#ifdef DEBUG_UIPANEL
        //# System.out.println(getClass().getName() + ".dispatchEventUIPanel() {");
        //#endif

        //#ifdef DEBUG_UIPANEL
        //# System.out.println("\te.getId() = " + e.getId());
        //# System.out.println("\te.getWParam() = " + e.getWParam());
        //# System.out.println("\te.getDWParam() = " + e.getDWParam());
        //#endif

	switch (e.getId()) {
	case Event.UI_MENU:
	    ret = eventUIMenu(e);
	    break;
	case Event.UI_ENTER:
	    break;
	case Event.UI_BACK:
	    break;
	case Event.UI_COMMAND:
	    ret = eventUICommand(e);
	    break;
	case Event.UI_KEY_RELEASED:
	    ret = eventUIKeyReleased(e);
	    break;
	}

        //#ifdef DEBUG_UIPANEL
        //# System.out.println("}");
        //#endif

	return ret;
    }

    protected void changeFocus(boolean value) {
	super.changeFocus(false);
    }

    protected void paintUIContainer(MGraphics g) {
        //#ifdef DEBUG_UIPANEL
        //# System.out.println(getClass().getName() + ".paintUIContainer() {");
        //#endif

	super.paintUIContainer(g);

	if (m_Soft != null) {
	    m_Soft.setPosX(getViewX());
	    m_Soft.setPosY(getViewY() + getViewHeight()); 
	    m_Soft.paint(g);
	}

        //#ifdef DEBUG_UIPANEL
        //# System.out.println("}");
        //#endif
    }

    /*
     * Events
     *
     */
    private boolean eventUIMenu(final Event e) {
	if (e.getWParam() == 0) {
	    setPopUp(null);
	    m_ShowMenu = false;
	}
	else {
	    setPositionMenu(m_Menu);
	    setPopUp(m_Menu);
	    m_ShowMenu = (m_Menu != null);
	}
	return true;
    }

    private boolean eventUICommand(final Event e) {
	boolean ret = false;
	UIButton button;
	
	if (m_Soft != null) {
	    // Recovery product by Id
	    button = m_Soft.getButton(e.getWParam());
	    // Recovery button by framework
	    //button = (UIButton)e.getObject();
	    if (button != null) {
		ret = true;
		switch (m_Soft.indexOf(button)) {
		case 0:
		    if (m_ShowMenu) {
			hideMenu();
		    }
		    else {
			showMenu();
		    }
		    break;
		case 1:
		    postEvent(Event.UI_ENTER);
		    break;
		case 2:
		    postEvent(Event.UI_BACK);
		    break;
		default:
		    ret = false;
		}
	    }
	}

	return ret;
    }

    private boolean eventUIKeyReleased(final Event e) {
	boolean ret = false;
	if (m_Soft != null) {
	    switch (e.getWParam()) {
	    case GUI.KEY_SOFTLEFT:
		ret = m_Soft.setFocusAt(0);
		break;
	    case GUI.KEY_OK:
		ret = m_Soft.setFocusAt(1);
		break;
	    case GUI.KEY_SOFTRIGHT:
		ret = m_Soft.setFocusAt(2);
		break;
	    }
	    if (ret) {
		e.setWParam(GUI.KEY_OK);
		m_Soft.dispatchEvent(e);
	    }
	}
	return ret;
    }
}
